package com.ccjk.beusoft.thirdlib;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.ccjk.beusoft.thirdlib.plugin1.hook.Hook;
import com.ccjk.beusoft.thirdlib.plugin1.PluginManager1;
import com.ccjk.beusoft.thirdlib.plugin2.PluginManager2;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Hook.get().hook(this);
        try {
            getPackageManager().getActivityLogo(getIntent());
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        //
        pluginApkPath = new File(getExternalCacheDir(), "plugin.apk").getAbsolutePath();
    }

    private void testHookAMS() {
        startActivity(new Intent(this, TargetActivity.class));
    }

    private String pluginApkPath;


    /**
     * @param view
     */
    public void loadPlugin(View view) {
        new Thread() {
            @Override
            public void run() {
                try {
                    Util.INSTANCE.copyFile(getAssets().open("plugindemo-debug.apk"), new File(pluginApkPath));
                    if (new File(pluginApkPath).exists()) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                loadPlugin();
                            }
                        });
                    } else throw new Exception("复制失败");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();


    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
//        PluginManager1.get().addLoadedApkToMap("beusoft.ccjk.com.plugindemo");

    }

    private void loadPlugin() {
//        try {
//            PluginManager1.get().loadApk(pluginApkPath);
//            PluginManager1.toast("插件加载成功");
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (NoSuchFieldException e) {
//            e.printStackTrace();
//        } catch (InstantiationException e) {
//            e.printStackTrace();
//        }
        try {
            PluginManager2.get().loadPlugin(pluginApkPath);
            Toast.makeText(this, "插件加载成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Log.e("load plugin","",e);
        }

    }

    public void startPluginActivity(View view) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("beusoft.ccjk.com.plugindemo", "beusoft.ccjk.com.plugindemo.MainActivity"));
        startActivity(intent);
//        Intent intent = new Intent(this, MainActivity.class);
        //      startActivity(intent);


    }
}
